<?php
/*****************************************************
 * File name: DataImport.php
 * Create date: 2019/03/28
 * Author: liuyue
 * Description: 版本模块cgi
 *****************************************************/

namespace Ctrl;

use Lib;
use Model\VersionModel;
use Model\ConfigModel;
use Lib\Service\JsonService;

class DataImport extends Lib\Base\Ctrl
{
    private $versionModel;
    private $configModel;
    private $tips =  ['type' => 0, 'status' => 'error', 'msg' => "输入不能为空"];

    public function __construct()
    {
        //校验管理员权限
        $roleService = new Lib\Paas\Role();
        $checkAdmin = $roleService->getRoleInfo();
//        if ($checkAdmin['role'] != ROLE_ID) {
//            die('抱歉！您没有访问权限');
//        }
//        if (!in_array(MODULE_ID, $checkAdmin['moduleID']) ) {
//            die('抱歉！您没有访问权限');
//        }
        //实例化模型
        $this->versionModel = new VersionModel();
        $this->configModel = new ConfigModel();
    }

    /**
     * 数据导入视图
     */
    public function index()
    {
        //获取用户信息
        $login = new Lib\Paas\Login();
        $loginInfo = $login->getLoginInfo();
        $this->assign("adminName",$loginInfo['name']);
        if(DB_HOST['db_name'] == 'dbTFTLibTest_xiangangwei'){
            $hostName = '当前是测试环境';
        }else{
            $hostName = '';
        }
        $this->assign('hostName',$hostName);
//        $this->display('Admin/DataImport/index');
        $this->display('Admin/Main/index');
    }

    /**
     * 数据导入主页面
     */
    public function main()
    {
        //判断是否有版本
        $isExistsVersion = $this->versionModel->isExistVersionData();

        //获取版本号
        $res = $this->versionModel->getCurrentVersion();

        //获取可切换的版本号
        $resTotal = $this->versionModel->getChangeVersions();

        //获取最新版本号
        $newVersion=  $this->versionModel->getLastVersion();

        //生成预存目录
        $saveDir = date('YmdHis',time());

        //获取版本数据导入前置数据
        $configItemsRes = $this->configModel->getConfigItems(['alias'=>'itemRemove'],'name,description,value');
        $configJsonRes = $this->configModel->getConfigItems(['alias'=>'checkJson'],'value');

        //数据
        $data = json_decode($configJsonRes['value'],true);
        $service = new JsonService();

        //遍历
        foreach ($data as $key=>$val){
            switch ($key)
            {
                //英雄Tmoba
                case $service::CATE_TMOBA_CHAMPION:
                    $heroTmobaType = $data[$key]['type'];
                    if($data[$key]['type'] === 1){
                        $heroTmobaTextArea = transformComma(implode(",",$data[$key]['data']));
                    }elseif($data[$key]['type'] === 2){
                        $heroTmobaKeys = $data[$key]['data'];
                    }
                    break;
                //道具Tmoba
                case $service::CATE_TMOBA_ITEM:
                    $itemsTmobaType = $data[$key]['type'];
                    if($data[$key]['type'] === 1){
                        $itemsTmobaTextArea = transformComma(implode(",",$data[$key]['data']));
                    }elseif($data[$key]['type'] === 2){
                        $itemsTmobaKeys = $data[$key]['data'];
                    }
                    break;
                //皮肤Tmoba
                case $service::CATE_TMOBA_SKINS:
                    $skinsTmobaType = $data[$key]['type'];
                    if($data[$key]['type'] === 1){
                        $skinsTmobaTextArea = transformComma(implode(",",$data[$key]['data']));
                    }elseif($data[$key]['type'] === 2){
                        $skinsTmobaKeys = $data[$key]['data'];
                    }
                    break;
                //英雄Json
                case $service::CATE_JSON_CHAMPION:
                    $heroJsonType = $data[$key]['type'];
                    if($data[$key]['type'] === 1){
                        $heroJsonTextArea = transformComma(implode(",",$data[$key]['data']));
                    }elseif($data[$key]['type'] === 2){
                        $heroJsonKeys = $data[$key]['data'];
                    }
                    break;
                //道具Json
                case $service::CATE_JSON_ITEM:
                    $itemsJsonType = $data[$key]['type'];
                    if($data[$key]['type'] === 1){
                        $itemsJsonTextArea = transformComma(implode(",",$data[$key]['data']));
                    }elseif($data[$key]['type'] === 2){
                        $itemsJsonKeys = $data[$key]['data'];
                    }
                    break;
                default:
                    break;
            }
        }

        $this->assign("saveDir",$saveDir);
        $this->assign("versionNumber",$res['versionNumber']);
        $this->assign("isExistsVersion",$isExistsVersion);
        $this->assign("newVersion",$newVersion['versionNumber']);
        $this->assign("itemsFilter",$configItemsRes);
        $this->assign("heroTmobaType",$heroTmobaType);
        $this->assign("heroTmobaTextArea",$heroTmobaTextArea);
        $this->assign("heroTmobaKeys",$heroTmobaKeys);
        $this->assign("itemsTmobaType",$itemsTmobaType);
        $this->assign("itemsTmobaTextArea",$itemsTmobaTextArea);
        $this->assign("itemsTmobaKeys",$itemsTmobaKeys);
        $this->assign("skinsTmobaType",$skinsTmobaType);
        $this->assign("skinsTmobaTextArea",$skinsTmobaTextArea);
        $this->assign("skinsTmobaKeys",$skinsTmobaKeys);
        $this->assign("heroJsonType",$heroJsonType);
        $this->assign("heroJsonTextArea",$heroJsonTextArea);
        $this->assign("heroJsonKeys",$heroJsonKeys);
        $this->assign("itemsJsonType",$itemsJsonType);
        $this->assign("itemsJsonTextArea",$itemsJsonTextArea);
        $this->assign("itemsJsonKeys",$itemsJsonKeys);
        $this->assign('resTotal',$resTotal);
        $this->display('Admin/DataImport/main');
    }

    /**
     * 版本数据导入前置处理逻辑
     */
    public function beforeImport()
    {
        $filterInfo=I('post.filterInfo');
        $filterArr =  json_decode(htmlspecialchars_decode($filterInfo),true);
        $filterInfo = $filterArr[0];

        $service = new JsonService();

        //道具过滤
        $itemsFilterValue = transformComma($filterInfo['itemsFilterValue']);
        $heroTmobaType = $filterInfo['heroTmobaType'];
        $itemsTmobaType = $filterInfo['itemsTmobaType'];
        $skinsTmobaType = $filterInfo['skinsTmobaType'];
        $heroJsonType = $filterInfo['heroJsonType'];
        $itemsJsonType = $filterInfo['itemsJsonType'];
        $db = Lib\Db\DbModel::init(); // 构造方法，线上自动切换
        $where['alias'] = 'itemRemove';
        $user = new Lib\Paas\Login();
        $userName = $user->getLoginInfo()['name'];

        $find = $db->table('tbconfig')->where($where)->find();
        $save['value'] = $itemsFilterValue;

        if($find){
            $save['updateMan'] = $userName;
            $save['updateTime'] = date("Y-m-d H:i:s", time());
            $res = $db->table('tbconfig')->where($where)->update($save);
            logWrite(2,$res,'tbconfig');
        }else{
            $save['creater'] = $userName;
            $save['createTime'] = date("Y-m-d H:i:s", time());
            $save['alias'] = 'itemRemove';
            $save['name'] = '道具过滤';
            $save['description'] = '设置需要过滤的道具，道具名称包含所设置的字符串将被过滤';
            $res = $db->table('tbconfig')->insert($save);
            logWrite(1,$res,'tbconfig');
        }
        unset($save);
        unset($where);
        if(!$res){
            $res = [
                'type' => 0,
                'status' => 'error',
                'msg' => "道具过滤设置失败"
            ];
            $this->ajaxReturn($res, 'json');
        }
        if($heroTmobaType=="1"){
            //获取英雄TMOBA里面的key过滤
            $heroTmobaTextArea = transformComma($filterInfo['heroTmobaTextArea']);
            if(empty($heroTmobaTextArea)||$heroTmobaTextArea === "")
            {
                $res = $this->tips;
                $this->ajaxReturn($res, 'json');
            }
            $item['type'] = 1;//key是否存在
            $key = explode(",",$heroTmobaTextArea);
            $item['data'] = $key;
            $data[$service::CATE_TMOBA_CHAMPION] = $item;//英雄 tmoba 1
            unset($key);
            unset($item);
        }elseif($heroTmobaType=="2"){
           //获取英雄TMOBA里面的key以及值过滤
            $heroTmobaKey = $filterInfo['heroTmobaKey'];
            $heroTmobaValue = $filterInfo['heroTmobaValue'];
            foreach ($heroTmobaKey as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            foreach ($heroTmobaValue as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            $heroTmobaArr=array_combine($heroTmobaKey,$heroTmobaValue);
            $item['type'] = 2;//key = value
            $item['data'] = $heroTmobaArr;
            $data[$service::CATE_TMOBA_CHAMPION] = $item;//英雄 tmoba 2
            unset($key);
            unset($item);
        }

        if($itemsTmobaType=="1"){
            //获取道具TMOBA里面的key过滤
            $itemsTmobaTextArea = transformComma($filterInfo['itemsTmobaTextArea']);
            if(empty($itemsTmobaTextArea)||$itemsTmobaTextArea === "")
            {
                $res = $this->tips;
                $this->ajaxReturn($res, 'json');
            }
            $item['type'] = 1;//key是否存在
            $key = explode(",",$itemsTmobaTextArea);
            $item['data'] = $key;
            $data[$service::CATE_TMOBA_ITEM] = $item;//道具 tmoba 1
            unset($key);
            unset($item);
        }elseif($itemsTmobaType=="2"){
            //获取道具TMOBA里面的key以及值过滤
            $itemsTmobaKey = $filterInfo['itemsTmobaKey'];
            $itemsTmobaValue = $filterInfo['itemsTmobaValue'];
            foreach ($itemsTmobaKey as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            foreach ($itemsTmobaValue as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            $itemsTmobaArr=array_combine($itemsTmobaKey,$itemsTmobaValue);
            $item['type'] = 2;//key = value
            $item['data'] = $itemsTmobaArr;
            $data[$service::CATE_TMOBA_ITEM] = $item;//道具 tmoba 2
            unset($key);
            unset($item);
        }

        if($skinsTmobaType=="1"){
            //获取皮肤TMOBA里面的key过滤
            $skinsTmobaTextArea = transformComma($filterInfo['skinsTmobaTextArea']);
            if(empty($skinsTmobaTextArea)||$skinsTmobaTextArea === "")
            {
                $res = $this->tips;
                $this->ajaxReturn($res, 'json');
            }
            $item['type'] = 1;//key是否存在
            $key = explode(",",$skinsTmobaTextArea);
            $item['data'] = $key;
            $data[$service::CATE_TMOBA_SKINS] = $item;//皮肤 tmoba 1
            unset($key);
            unset($item);
        }elseif($skinsTmobaType=="2"){
            //获取皮肤TMOBA里面的key以及值过滤
            $skinsTmobaKey = $filterInfo['skinsTmobaKey'];
            $skinsTmobaValue = $filterInfo['skinsTmobaValue'];
            foreach ($skinsTmobaKey as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            foreach ($skinsTmobaValue as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            $skinsTmobaArr=array_combine($skinsTmobaKey,$skinsTmobaValue);
            $item['type'] = 2;//key = value
            $item['data'] = $skinsTmobaArr;
            $data[$service::CATE_TMOBA_SKINS] = $item;//皮肤 tmoba 2
            unset($key);
            unset($item);
        }

        if($heroJsonType=="1"){
            //获取英雄JSON里面的key过滤
            $heroJsonTextArea = transformComma($filterInfo['heroJsonTextArea']);
            if(empty($heroJsonTextArea)||$heroJsonTextArea === "")
            {
                $res = $this->tips;
                $this->ajaxReturn($res, 'json');
            }
            $item['type'] = 1;//key是否存在
            $key = explode(",",$heroJsonTextArea);
            $item['data'] = $key;
            $data[$service::CATE_JSON_CHAMPION] = $item;//英雄 json 1
            unset($key);
            unset($item);
        }elseif($heroJsonType=="2"){
            //获取英雄JSON里面的key以及值过滤
            $heroJsonKey = $filterInfo['heroJsonKey'];
            $heroJsonValue = $filterInfo['heroJsonValue'];
            foreach ($heroJsonKey as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            foreach ($heroJsonValue as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            $heroJsonArr=array_combine($heroJsonKey,$heroJsonValue);
            $item['type'] = 2;//key = value
            $item['data'] = $heroJsonArr;
            $data[$service::CATE_JSON_CHAMPION] = $item;//英雄 json 2
            unset($key);
            unset($item);
        }

        if($itemsJsonType=="1"){
            //获取道具JSON里面的key过滤
            $itemsJsonTextArea = transformComma($filterInfo['itemsJsonTextArea']);
            if(empty($itemsJsonTextArea)||$itemsJsonTextArea === "")
            {
                $res = $this->tips;
                $this->ajaxReturn($res, 'json');
            }
            $item['type'] = 1;//key是否存在
            $key = explode(",",$itemsJsonTextArea);
            $item['data'] = $key;
            $data[$service::CATE_JSON_ITEM] = $item;//道具 json 1
            unset($key);
            unset($item);
        }elseif($itemsJsonType=="2"){
            //获取道具JSON里面的key以及值过滤
            $itemsJsonKey = $filterInfo['itemsJsonKey'];
            $itemsJsonValue = $filterInfo['itemsJsonValue'];
            foreach ($itemsJsonKey as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            foreach ($itemsJsonValue as $key=>$val){
                if(empty($val)||$val === ""){
                    $res = $this->tips;
                    $this->ajaxReturn($res, 'json');
                }
            }
            $itemsJsonArr=array_combine($itemsJsonKey,$itemsJsonValue);
            $item['type'] = 2;//key = value
            $item['data'] = $itemsJsonArr;
            $data[$service::CATE_JSON_ITEM] = $item;//道具 json 2
            unset($key);
            unset($item);
        }
        //初始数据
        $where['alias'] = 'checkJson';
        $find = $db->table('tbconfig')->where($where)->find();
        $save['value'] = json_encode($data);

        if($find){
            $save['updateMan'] = $userName;
            $save['updateTime'] = date("Y-m-d H:i:s", time());
            $res = $db->table('tbconfig')->where($where)->update($save);
            logWrite(2,$res,'tbconfig');

        }else{
            $save['creater'] = $userName;
            $save['createTime'] = date("Y-m-d H:i:s", time());
            $save['alias'] = 'checkJson';
            $save['name'] = 'json文件匹配规则';
            $save['description'] = '系统将根据这个规则来检测json文件的类型';
            $res = $db->table('tbconfig')->insert($save);
            logWrite(1,$res,'tbconfig');
        }

        if($res){
            $res = [
                'type' => 0,
                'status' => 'success',
                'msg' => "版本数据前置配置成功"
            ];
            $this->ajaxReturn($res, 'json');
        }else{
            $res = [
                'type' => 0,
                'status' => 'error',
                'msg' => "版本数据前置配置失败"
            ];
            $this->ajaxReturn($res, 'json');
        }
        exit;
    }
}